/**
 * Created by yangl on 2017/8/16.
 */
var productDetailController={
    init:function(){
        //试用
        $("#tryproduct").click(function(){
            productDetailController.judgtry();
        });

        $("#minusbuynum").click(function(){
            var num = $("#buynum").val();
            num = parseInt(num);
            if(num == 1){
                num = 1;
            }else{
                if((num-1)<1){
                    num = 1;
                }else{
                    num = num-1;
                }
            }
            $("#buynum").val(num);
            $("#totalprice").html("￥"+parseFloat(num*price).toFixed(2));
        });

        $("#addbuynum").click(function(){
            var num = $("#buynum").val();
            num = parseInt(num);
            num = num+1;
            $("#buynum").val(num);
            $("#totalprice").html("￥"+parseFloat(num*price).toFixed(2));
        });

        $("#confimbuy").click(function(){
            productDetailController.buyproduct();
        });
    },
    judgtry:function(){
        var productId = $("#productId").val();
        var data = {
            productId:productId
        };
        $.ajax({
            type: "post",
            url: "/custom/product/data/trynum",
            dataType: "json",
            data:data,
            success: function(json){
                if(json == 2){
                    alert("您的试用次数已超限，请购买");
                }else if(json == 100){
                    alert("提交失败");
                }else{
                    window.location.href = basePath + "/custom/product/page/try/"+productId;
                }
            },
            failure:function (result) {
                console.log("提交失败");
            }
        });
    },
    buyproduct:function(){
        var form = {};
        form.type=0;//购买订单
        form.price = price;
        form.number = $("#buynum").val();
        form.productId = $("#productId").val();
        $.ajax({
            type: "post",
            url: "/custom/product/data/createorder",
//                cache:false,
//                async:false,
//             dataType: "json",
            data:form,
            success: function(json){
                // console.log("json"+json);
                if(json == "提交成功"){
                    $('#myModal').modal('show')
                    $('#myModal').on('hide.bs.modal', function () {
                        window.location.href=basePath+"/custom/usercenter/page?type=tabs2";
                    })
                }else{
                    alert(json);
                }
            },
            failure:function (result) {
                console.log("提交失败");
            }
        });
    }
}

$(function(){
    productDetailController.init();
})
